package com.hspedu.newData.dmsxl.哈希表;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-04 
 * @Version: 1.0
 */

// todo
public class LC1128等价多米诺骨牌对的数量 {

    public int numEquivDominoPairs(int[][] dominoes) {

        Map<String,Integer> map = new HashMap<>();
        for (int i = 0; i < dominoes.length; i++) {
            String nums2 = dominoes[i][1] + dominoes[i][0] + "";
            String nums1 = dominoes[i][0] + dominoes[i][1] + "";
            if (map.containsKey(nums1) || map.containsKey(nums2)){
                String x = map.containsKey(nums1)? nums1: nums2;
                map.put(x,map.get(x) + 1);
                continue;
            }
            map.put(nums1,1);
        }

        int res = 0;
        for (Integer value : map.values()) {
            if (value == 1){
                continue;
            }
            res += value*(value-1)/2;
        }
        return res;
    }

    @Test
    public void test() {
        // 测试代码

        numEquivDominoPairs(new int[][]{{1,2},{1,2},{1,1},{1,2},{2,2}});

    }

}
